<html xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:py="http://genshi.edgewall.org/" py:strip="">
  <?python
    from trac.wiki.formatter import format_to_oneliner, format_to_html
    from trac.util.datefmt import format_datetime, pretty_timedelta
  ?>

  <py:def function="sortable_th(order, desc, Class, title, href)">
    <th class="${Class}${order == Class and (desc and ' desc' or ' asc') or ''}">
      <a title="Sort by ${Class}${(order == Class and desc) and ' (descending)' or ''}" href="${href}?order=${Class}&amp;desc=${(Class == order and desc) and '0' or '1'}">
        $title
      </a>
    </th>
  </py:def>

  <py:def function="sortable_th_nc(order, desc, Class, title)">
    <th class="${Class}${order == Class and (desc and ' desc' or ' asc') or ''}">
        $title
    </th>
  </py:def>




<!--  show discussions main part -->
    <py:def function="display_discussion(discussion, href)">

        <py:with vars="is_topic_edit = not req.args.message and (req.args.discussion_action in ('edit', 'post-edit')) and not req.args.submit;
                   is_topic_add = not req.args.message and (req.args.discussion_action in ('add', 'quote', 'post-add')) and not req.args.submit">

            <div class="topic ${(discussion.topic.time > discussion.visit_time) and 'new' or None}">
                <py:if test="discussion.paginator.page == 0">
                  <div class="header" py:choose="">
                      <py:when test="is_topic_edit">
                          <a name="preview"></a>
                          <div class="subject">
                          	     <py:with vars="postnumber = 1; pageinfo = ' - page 1 of %s' % (discussion.paginator.num_pages)">
                        		  	<span class="postnumber">#${postnumber}<span class="pageinfo">${pageinfo}</span></span>
                                  </py:with>
                                  
                                  ${req.args.subject or ''} - ${fromtimestamp(discussion.topic.time).strftime("%Y-%m-%d %H:%M")}
					              ${display_topic_status(discussion.topic.status)}
                          </div>
                          <div class="body">
                              <div class="author">
                                  <xi:include href="disc_userbadge.html" />
                              </div>
                              <div class="bodytext">
                                  ${format_to_html(discussion.env, context, req.args.body) or '&nbsp;'}
                              </div>
                          </div>
                      </py:when>
                      <py:otherwise>
                          <div class="subject" id="topic_subject">
                                <span py:for="visualstatus in discussion.visualstatus" style="font-weight:bold;float:left;" id="${visualstatus}">${visualstatus}</span>
                                <span style="font-weight:bold;float:left;">${visualstatus}</span>
								<py:with vars="postnumber = 1; pageinfo = ' - page 1 of %s' % (discussion.paginator.num_pages)">
                        		  	<span class="postnumber">#${postnumber}<span class="pageinfo">${pageinfo}</span></span>
                                </py:with>
                                ${discussion.topic.subject or 'No subject'} - ${fromtimestamp(discussion.topic.time).strftime("%Y-%m-%d %H:%M")}
                                  
                          </div>
                          <div class="body">
                              <div class="author">
                                  <xi:include href="disc_userbadge.html" />
                              </div>
                              <div class="bodytext" style="min-height:130px;">
                                  ${format_to_html(discussion.env, context, discussion.topic.body) or '&nbsp;'}
                              </div>
                              <div class="controls">
                                  <py:if test="((discussion.forum.name != conf.news_forum_name and 'DISCUSSION_APPEND' in perm) or (discussion.forum.name == conf.news_forum_name and 'DISCUSSION_ANNOUNCEAPPEND' in perm)) and 'locked' not in discussion.topic.status">

                                      <a href="${href}?discussion_action=add;#addmessage">Reply</a>

                                      <span class="controldot"></span>
                                      <a href="${href}?discussion_action=quote;#addmessage">Quote</a>

                                      <py:if test="('DISCUSSION_MODERATE' in perm and discussion.moderator) or ((discussion.authname == discussion.topic.author) and (discussion.authname != 'anonymous'))">
                                      	<span class="controldot"></span>
                                      	<a href="${href}?discussion_action=edit;#topic">Edit</a>
                                      </py:if>
                                      
                                  </py:if>
                              </div>
                          </div>
                      </py:otherwise>
                  </div>
                </py:if>
            </div>      

            <py:if test="is_topic_edit">
                ${display_form(href, 'Edit:', 'edit_form', 'post-edit')}
            </py:if>

            <div py:if="len(discussion.messages) or is_topic_add" class="replies">
                <py:if test="len(discussion.messages)">
                    ${display_messages(href, discussion.messages)}
                </py:if>
                <py:if test="req.args.discussion_action not in ('edit', 'post-edit') and 'locked' not in discussion.topic.status">
                    ${display_preview()}
                    ${display_form(href, 'Reply:', 'add_form', 'post-add')}
                </py:if>
            </div>

            <xi:include py:with="paginator = discussion.paginator" href="page_index.html" />

      <div py:if="('DISCUSSION_ATTACH' in perm)" class="attachments">
        <a name="attachments"></a>
        <py:choose>
          <py:when test="not len(discussion.attachments.attachments)">
            <h3>Attachments</h3>
            <p>No attachments created.</p>
            ${attach_file_form(discussion.attachments)}
          </py:when>
          <py:otherwise>
            ${list_of_attachments(discussion.attachments, compact = True)}
            ${attach_file_form(discussion.attachments)}
          </py:otherwise>
        </py:choose>
      </div>


      <div py:if="discussion.moderator or discussion.authemail"
        class="subscription" style="margin:10px;">
        <a name="subscription"></a>
        <form method="post" action="${href}">
          <fieldset id="subscription">

            <div py:if="discussion.moderator">
            <legend>Manage subscribers</legend>

              <br/>

              <py:choose>
                <py:when test="(len(discussion.topic['subscribers']) == 0)">
                  <div id="nosubs" name="nosubs" style="display:block;">
                  No subscribers
                  </div>
                </py:when>
                <py:otherwise>
                  <div id="nosubs" name="nosubs" style="display:none;">
                  No subscribers
                  </div>
                </py:otherwise>
              </py:choose>

              <py:choose>
                <select py:when="(len(discussion.topic.subscribers) == 0)" id="subscribers"
                  name="subscribers" multiple="on" style="display:none;">
                </select>
                <select py:when="(len(discussion.topic.subscribers) > 0) and (7 >
                  len(discussion.topic.subscribers))" id="subscribers"
                  name="subscribers" multiple="on">
                  <py:for each="user in discussion.topic.subscribers">
                    <py:choose>
                      <option value="$user" selected="selected">$user</option>
                    </py:choose>
                  </py:for>
                </select>
                <select py:when="len(discussion.topic.subscribers) > 6" id="subscribers"
                  name="subscribers" size="6" multiple="on">
                  <py:for each="user in discussion.topic.subscribers">
                    <py:choose>
                      <option value="$user" selected="selected">$user</option>
                    </py:choose>
                  </py:for>
                </select>
              </py:choose>
              <br/>
            </div>

            <div py:if="discussion.moderator">
              <label for="unregistered_subscribers">Add subscribers (usernames or email addresses):</label>
              <br/>
              <input type="text" id="unregistered_subscribers"
                name="unregistered_subscribers" />
                <span class="secondaryButton">
                  <input type="button" id="addsubscriber" name="addsubscriber" value="Add" style="width:120px;"/>
                </span> 

              <br/>
            </div>

            <div py:if="discussion.moderator" class="buttons">
              <input type="hidden" name="discussion_action"
                value="subscriptions-post-edit"/>
                <span class="secondaryButton">
                    <input type="submit" name="editsubscribers" value="Save" style="width:120px;"/>
                </span> 
            </div>
          </fieldset>
        </form>
      </div>

      <div py:if="discussion.moderator or ((discussion.authname ==
        discussion.topic.author) and (not 'locked' in discussion.topic.status)
        and (discussion.authname != 'anonymous'))" class="moderation" style="margin:10px;">
        <a name="moderation"></a>
        <form method="post" action="${req.href.discussion('topic',
          discussion.topic.id)}">
          <fieldset id="moderation">
            <legend>Moderation</legend>
            <div py:if="discussion.moderator">
              <label for="important">
                <input py:if="discussion.topic.priority > 0" type="checkbox"
                  name="important" id="important" checked="yes" value="1"
                  onclick="change_attribute('${req.href.discussion('ajax',
                    'topic', discussion.topic.id)}', 'important',
                    this.checked);"/>
                <input py:if="not (discussion.topic.priority > 0)" type="checkbox"
                  name="important" id="important" value="0"
                  onclick="change_attribute('${req.href.discussion('ajax',
                    'topic', discussion.topic.id)}', 'important',
                    this.checked);"/>
                Make this topic sticky at the top.
              </label><br/>
            </div>
            <div>
              <label for="solved">
                <input py:if="'solved' in discussion.topic.status" type="checkbox"
                  name="solved" id="solved" checked="yes" value="1"
                  onclick="change_attribute('${req.href.discussion('ajax',
                    'topic', discussion.topic.id)}', 'status.solved',
                    this.checked);"/>
                <input py:if="not 'solved' in discussion.topic.status" type="checkbox"
                  name="solved" id="solved" value="0"
                  onclick="change_attribute('${req.href.discussion('ajax',
                    'topic', discussion.topic.id)}', 'status.solved',
                    this.checked);"/>
                Solve topic.
              </label><br/>
            </div>
            <div py:if="discussion.moderator">
              <label for="locked">
                <input py:if="'locked' in discussion.topic.status" type="checkbox"
                  name="locked" id="locked" checked="yes" value="1"
                  onclick="change_attribute('${req.href.discussion('ajax',
                    'topic', discussion.topic.id)}', 'status.locked',
                    this.checked); lock_topic(this.checked);"/>
                <input py:if="not 'locked' in discussion.topic.status"
                  type="checkbox" name="locked" id="locked" value="0"
                  onclick="change_attribute('${req.href.discussion('ajax',
                    'topic', discussion.topic.id)}', 'status.locked',
                    this.checked); lock_topic(this.checked);"/>
                Lock topic.
              </label>
            </div>
            <input type="hidden" name="discussion_action" value="edit-attribute"/>
          </fieldset>
        </form>
      </div>


    </py:with>
</py:def>









<!--  form -->
    <py:def function="display_form(href, title, Class, action)">
        <a name="addmessage"></a>
        <div id="messageaddbox" py:if="(discussion.forum.name != conf.news_forum_name and 'DISCUSSION_APPEND' in perm) or (discussion.forum.name == conf.news_forum_name and 'DISCUSSION_ANNOUNCEAPPEND' in perm)">
        <form class="${Class}" method="post" action="${href}#${discussion.message.id and ('message' + unicode(discussion.message.id)) or 'topic'}">
            <fieldset>
                <div class="field">
                    <py:if test="req.authname == 'anonymous'">
                        <span>You are posting as anonymous, if you have an account we recommend that you <a href="${conf.url_home_path}/user">log in</a>.</span>
                    </py:if>
                    <input type="hidden" id="author" name="author" value="${req.authname}" />
                </div>

                <div py:if="is_topic_edit" class="field">
                    <label for="subject">Subject:</label>
                    <br/>
                    <py:choose>
                        <input py:when="discussion.realm == 'discussion-wiki'" type="text" id="subject" name="subject" value="${req.args.subject}" readonly="readonly"/>
                        <input py:otherwise="" type="text" id="subject" name="subject" value="${req.args.subject}"/>
                    </py:choose>
                    <br/>
                </div>

     			<h2>Reply to Thread</h2>
     			<div>
	            	<textarea id="add_msg_body" name="body"
	                       class="wikitext" rows="20" cols="131">${req.args.body or ''}</textarea>
				</div>
               <div class="buttons">
                    <span style="float:right;">
                    <py:choose>
                        <py:when test="req.args.discussion_action in ('edit', 'post-edit')">
                            <input type="submit" name="submit" value="Save" />
                        </py:when>
                        <py:otherwise>
                            <input type="submit" name="submit" value="Reply to Thread" />
                        </py:otherwise>
                    </py:choose>
                    </span>
                </div>

                <input py:if="req.args.message" type="hidden" name="message" value="${req.args.message}"/>
                <input type="hidden" name="discussion_action" value="${action}"/>
            </fieldset>
        </form>
        </div>
    </py:def>

<!--  display preview -->
    <py:def function="display_preview()">
        <div py:if="req.args.preview" class="preview">
            <div class="id">
                Message #??
            </div>
            <div class="body">
                ${format_to_html(discussion.env, context, req.args.body or '&nbsp;')}
            </div>
            <div class="footer">
                <div class="author">
                    ${req.authname}
                </div>
                <div class="time">
                    ${format_datetime(discussion.time)} (${pretty_timedelta(discussion.time)} ago)
                </div>
            </div>
        </div>
    </py:def>




<!--  display messages -->
    <py:def function="display_messages(href, messages)">
        <div py:for="line, message in enumerate(messages)" class="${(message.time > discussion.visit_time) and 'new' or None} ${line % 2 and 'discmessage_even' or 'discmessage_odd'}">
            <py:with vars="is_message_edit = (str(req.args.message) == str(message.id)) and (req.args.discussion_action in ('edit', 'post-edit')) and not req.args.submit;
                            last_message = len(messages)-1">
                <a name="message${message.id}"></a>
                <a py:if="line == last_message" name="lastmessage"></a>
                  <div class="subject">
                  	<py:with vars="postnumber = discussion.paginator.page * discussion.paginator.max_per_page + line + (discussion.paginator.page == 0 and 2 or 1);
                        	           pageinfo = ' - page %s of %s' % (discussion.paginator.page + 1, discussion.paginator.num_pages)">
                        <span class="postnumber">#${postnumber}<span class="pageinfo">${pageinfo}</span></span>
                    </py:with>
                  	Re: ${discussion.topic.subject or ''} - ${fromtimestamp(message.time).strftime("%Y-%m-%d %H:%M")}
                  </div>

                <div class="body">

                    <div class="author">
                        <xi:include href="disc_userbadge.html" />
                    </div>

                    <a py:if="is_message_edit" name="preview"></a>

                    <div class="bodytext" py:choose="">
                        <py:when test="is_message_edit">
                            ${format_to_html(discussion.env, context, req.args.body or '&nbsp;')}
                        </py:when>
                        <py:otherwise>
                            ${format_to_html(discussion.env, context, message.body or '&nbsp;')}
                        </py:otherwise>
                    </div>
                </div>

                <div class="controls">
                    <py:if test="((discussion.forum.name != conf.news_forum_name and 'DISCUSSION_APPEND' in perm) or (discussion.forum.name == conf.news_forum_name and 'DISCUSSION_ANNOUNCEAPPEND' in perm)) and 'locked' not in discussion.topic.status">
                        <a href="${href}?discussion_action=add;message=${message.id}#message${message.id}">Reply</a>
                        <span class="controldot"></span>
                        <a href="${href}?discussion_action=quote;message=${message.id}#message${message.id}">Quote</a>
                        <span py:if="('DISCUSSION_MODERATE' in perm and discussion.moderator) or ((discussion.authname == message.author) and (discussion.authname != 'anonymous'))" class="controldot"></span>
                        <a py:if="('DISCUSSION_MODERATE' in perm and discussion.moderator) or ((discussion.authname == message.author) and (discussion.authname != 'anonymous'))" href="${href}?discussion_action=edit;message=${message.id}#message${message.id}">Edit</a>
                    </py:if>
                    <py:if test="'locked' not in discussion.topic.status">
                      <span py:if="'DISCUSSION_MODERATE' in perm and discussion.moderator" class="controldot"></span>
                      <a py:if="'DISCUSSION_MODERATE' in perm and discussion.moderator" href="${href}?discussion_action=delete;message=${message.id}#${message.replyto == -1 and 'topic' or ('message' + unicode(message.replyto))}" onclick="return confirm('Do you realy want to delete this reply and all its descendants?')">Delete</a>
                    </py:if>
                </div>
                <div class="footer"></div>
                <div style="clear:both;">
                <py:if test="(str(req.args.message) == str(message.id)) and (not req.args.submit)">
                    <py:if test="req.args.discussion_action in ('edit', 'post-edit') and 'locked' not in discussion.topic.status">
                        ${display_form(href, 'Edit:', 'edit_form', 'post-edit')}
                    </py:if>
                </py:if>
                </div>
        </py:with>

        </div>
    </py:def>

 <py:def function="display_topic_status(status)">
   <div py:for="state in status" class="icon ${state}">
     &nbsp;<span class="icon-text ${state}">${state}</span>
   </div>
 </py:def>

</html>
